<input type="file" id="fileInput" accept=".docx" />
<div id="output"></div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/mammoth/1.4.2/mammoth.browser.min.js"></script>
<script>
  document.getElementById("fileInput").addEventListener("change", function (event) {
    const file = event.target.files[0];

    if (!file) {
      console.error("No file selected");
      return;
    }

    if (file.type !== "application/vnd.openxmlformats-officedocument.wordprocessingml.document") {
      console.error("Please upload a valid .docx file");
      return;
    }

    // 读取文件并传递给 mammoth
    const reader = new FileReader();
    reader.onload = function (e) {
      const arrayBuffer = e.target.result;
      console.log(e.target)
      mammoth
        .extractRawText({ arrayBuffer })
        .then(function (result) {
          console.log(result)
          document.getElementById("output").innerText = result.value; // 提取的文本
        })
        .catch(function (err) {
          console.error("Error extracting text:", err);
        });
    };

    reader.onerror = function () {
      console.error("Error reading file");
    };

    reader.readAsArrayBuffer(file); // 读取文件为 ArrayBuffer
  });
</script>